﻿using UnityEngine;
using System.Collections.Generic;
using System.Linq;

public class HighScoreManager : MonoBehaviour {

    public static void AddHighScore(int score)
    {
        List<int> HList = HighScoreList();
        if (HList.Any(h => h < score))
        {
            HList.Add(score);
            HList.Sort();
            HList.RemoveAt(0);
            HList.Reverse();
        }
        if (HList.First() == score)
        {
            PlayerPrefs.SetInt("NewHigh", 1);
        }
        else
        {
            PlayerPrefs.SetInt("NewHigh", 0);
        }
        for (int i = 0; i < 5; i++)
        {
            PlayerPrefs.SetInt("HighScore" + (i + 1), HList[i]);
        }
        PlayerPrefs.Save();
    }

    public static void ClearHighScores()
    {
        PlayerPrefs.DeleteAll();
    }

    public static List<int> HighScoreList()
    {
        List<int> HList = new List<int>();
        for (int i = 1; i < 6; i++)
        {
           string currentScore = "HighScore" + i;
           HList.Add(PlayerPrefs.GetInt(currentScore));
        }
        return HList;
    }
}
